package io.shallwe.scanner.auto;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Date;

import com.jfinal.plugin.activerecord.Model;
import io.shallwe.common.model.ScannerRecord;
import org.apache.commons.lang3.StringUtils;
import org.tio.core.ChannelContext;
import org.tio.core.TioConfig;
import org.tio.core.exception.AioDecodeException;
import org.tio.core.intf.Packet;
import org.tio.server.intf.ServerAioHandler;

import com.jfinal.aop.Aop;
import com.jfinal.kit.LogKit;

import io.shallwe._admin.basedata.scanner.ScannerService;
import io.shallwe._admin.line.EnterScanService;
import io.shallwe._admin.line.ExitScanService;
import io.shallwe.common.model.Scanner;
import io.shallwe.scanner.manual.MessagePacket;

/**
 *   自动扫描枪数据处理器
 * @author 王小伟
 * @date 2016-06-16 00:21:11
 */
public class AutoScannerHandler implements ServerAioHandler {

	 ScannerService scannerService=Aop.get(ScannerService.class);

	 EnterScanService enterScanSerice=Aop.get(EnterScanService.class);

	 ExitScanService exitScanService=Aop.get(ExitScanService.class);


	@Override
	public Packet decode(ByteBuffer buffer, int limit, int position, int readableLength, ChannelContext channelContext){
		if (readableLength < 3) {
			return null;
		}
		MessagePacket packet = new MessagePacket();
		byte[] body = new byte[readableLength];
		buffer.get(body);
		packet.setBody(body);
		return packet;
	}

	@Override
	public ByteBuffer encode(Packet packet, TioConfig config, ChannelContext channelContext) {
		if (packet instanceof MessagePacket) {
			MessagePacket helloPacket = (MessagePacket) packet;
			byte[] body = helloPacket.getBody();
			ByteBuffer buffer = ByteBuffer.allocate(body.length);
			buffer.order(config.getByteOrder());
			if (body != null) {
				buffer.put(body);
			}
			return buffer;
		}
		return null;
	}

	@Override
	public void handler(Packet packet, ChannelContext channelContext)  {
		if (packet instanceof MessagePacket) {
			MessagePacket messagePacket = (MessagePacket) packet;
			byte[] data = messagePacket.getBody();
			if (data == null) {
				return;
			}
			String ip = channelContext.getClientNode().getIp();
			String hangerNo = new String(data, Charset.forName("UTF8"));
			LogKit.error(String.format("收到自动扫描枪:%s的数据:%s", ip, hangerNo));
			hangerNo = StringUtils.remove(hangerNo, (char) 0x1B);
			hangerNo = StringUtils.remove(hangerNo, (char) 0x20);
			hangerNo = StringUtils.remove(hangerNo, (char) 0x0D);
			hangerNo = StringUtils.remove(hangerNo, (char) 0x02);
			hangerNo = StringUtils.remove(hangerNo, (char) 0x03);
			hangerNo = StringUtils.remove(hangerNo, (char) 0x04);
			if(StringUtils.equals(hangerNo, "9999")) {
				return;
			}
			Scanner scanner = scannerService.findByIp(ip);
			ScannerRecord record = new ScannerRecord();
			record.setRawText(hangerNo);
			record.setHangerNo(hangerNo);
			record.setScannerId(scanner.getId())
					.setScannerType(scanner.getScannerType())
					.setStatusFlag(ScannerRecord.INIT)
					.setManualFlag(ScannerRecord.SYSTEM_SCAN)
					.setCreateTime(new Date())
					.setUpdateTime(new Date());
			record.save();
			if (scanner.isAutoEnter()) {
				enterScanSerice.autoScan(hangerNo);
			} else if (scanner.isAutoExit()) {
				exitScanService.autoScan(hangerNo);
			} else {
				return;
			}
		} else {
			return;
		}
	}


}
